<template>
  <div class="tab-container">
    <el-tabs
      class="fullheight tab-container-root"
      :class="{'tab-default': !finalTabsStyle.tabType}"
      v-model="currentTabComponent"
      :type="finalTabsStyle.tabType"
      @tab-click="handlePaneClick"
      v-if="tabComponentOptions.length > 0"
    >
      <el-tab-pane
        :name="tabComponent.alias"
        v-for="(tabComponent, index) in tabComponentOptions"
        :key="index"
        :class="{'not-aside-tree': !hasTreeComponents(tabComponent.alias)}"
      >
        <span slot="label">
          <span class="ht-menu_title">
            <el-link
              :underline="false"
              class="ht-menu_title_item"
              :href="genHref(tabComponent.alias)"
              >{{ tabComponent.name }}
            </el-link>
          </span>
        </span>

        <component
          v-if="currentTabComponent == tabComponent.alias && !tabComponent.href"
          v-bind:is="currentTabComponent"
        ></component>
        <iframe
          v-if="tabComponent.href"
          :id="tabComponent.id"
          style="height: 100%; width: 100%; border: 0px"
          :src="tabComponent.href"
        ></iframe>
      </el-tab-pane>
    </el-tabs>

    <component
      v-if="tabComponentOptions.length <= 0"
      v-bind:is="currentTabComponent"
    ></component>
  </div>
</template>

<script>
import {mapGetters} from 'vuex'
import utils from '@/hotent-ui-util.js'

// 懒加载组件 只加载当前页面用到的组件
/** 排课相关页面 **/

const demo = () => import('@/views/Demo.vue')
const ruleManager =  () => import('@/views/course/rule/RuleManager.vue')
const recordManager =  () => import('@/views/course/record/RecordManager.vue')
const propertyManager =  () => import('@/views/course/property/PropertyManager.vue')

import menuApi from '@/api/menu.js'

const IGNORE_ALIAS = []

export default {
  name: 'ht-tabs-component',
  components: {
    demo,
    ruleManager,
    recordManager,
    propertyManager
  },
  props: {},
  computed: {
    ...mapGetters({
      tabsStyle: 'styleSetting/tabsStyle'
    }),
    finalTabsStyle: function() {
      // 使用菜单的配置
      if (
        (this.tabsStyle.tabType == 'menuSetting' ||
          this.tabsStyle.tabType == null) &&
        this.menuTabsStyle
      ) {
        return this.menuTabsStyle
      }

      // 使用默认的
      if (
        this.tabsStyle.tabType == null ||
        ((this.tabsStyle.tabType == 'menuSetting' ||
          this.tabsStyle.tabType == null) &&
          !this.menuTabsStyle)
      ) {
        return {tabType: ''}
      }
      // 使用全局的配置
      return this.tabsStyle
    },
    hasTreeComponents() {
      return alias => {
        const aliasList = [

        ]
        return aliasList.includes(alias)
      }
    }
  },
  data() {
    return {
      tabComponentOptions: [],
      currentTabComponent: '',
      menuTabsStyle: null
    }
  },
  methods: {
    handlePaneClick: function(data) {
      if (data.alias) {
        this.currentTabComponent = data.alias
      }
    },
    genHref(anchor) {
      if (this.$router.mode == 'history') {
        return '#' + anchor
      }
      return '/#' + this.$route.path + '#' + anchor
    },
    activeTab(fullpath) {
      let lastIdx = fullpath.lastIndexOf('#')
      if (lastIdx != -1) {
        let urlAnchor = fullpath.substr(lastIdx + 1)
        this.tabComponentOptions.filter(item => {
          if (item.alias == urlAnchor) {
            this.currentTabComponent = urlAnchor
            console.log(1, this.currentTabComponent)
          }
        })
      }
    }
  },
  mounted() {},
  beforeRouteEnter(to, from, next) {
    next(vm => {
      // 通过 `vm` 访问组件实例
      let routePath = vm.$route.path
      if (routePath) {
        routePath = routePath.replace('/', '')
      }
      menuApi.getMenuByAlias(routePath, m => {
        if (m) {
          vm.$emit('menuChange', m)
          let children = m.children
            ? m.children.filter(item => {
                return !IGNORE_ALIAS.includes(item.alias)
              })
            : []
          if (children && children.length > 0) {
            vm.tabComponentOptions = children
            vm.currentTabComponent =
              m.activeTab || vm.tabComponentOptions[0].alias
            vm.activeTab(to.fullPath)
          } else {
            vm.tabComponentOptions = []
            vm.currentTabComponent = m.alias
          }
          if (m.tabsStyle) {
            vm.menuTabsStyle = utils.parseToJson(m.tabsStyle)
          } else {
            vm.menuTabsStyle = null
          }
        } else {
          // 没有菜单的路由跳转   需要显示TAb名称需要定义tobName参数传递过来
          vm.tabComponentOptions = []
          vm.currentTabComponent = to.name
          vm.$emit('menuChange', {name: to.params.tobName})
        }
      })
    })
  },
  beforeRouteUpdate(to, from, next) {
    // 在当前路由改变，但是该组件被复用时调用
    // 举例来说，对于一个带有动态参数的路径 /foo/:id，在 /foo/1 和 /foo/2 之间跳转的时候，
    // 由于会渲染同样的 Foo 组件，因此组件实例会被复用。而这个钩子就会在这个情况下被调用。
    // 可以访问组件实例 `this`
    this.activeTab(to.fullPath)
    next()
  }
}
</script>

<style lang="scss" scoped>
@import '@/assets/css/element-variables.scss';

.tab-container >>> .is-active .el-link {
  color: $--color-primary;
  font-weight: bold;
}
.tab-container .el-tab-pane {
  height: 100%;
}
// 表单内边距
.tab-container {
  padding: 24px;
  .not-aside-tree {
    padding: 9px 24px 20px 24px;
  }
  ::v-deep {
    .el-tabs__content {
      height: calc(100% - 60px);
      .pagination-panel {
        padding-bottom: 0;
      }
    }
    .el-tabs--card {
      padding: 24px 24px 0px 24px;
      .el-tabs__content {
        height: calc(100% - 102px);
      }
      .not-aside-tree {
        padding: 0 0 20px 0;
      }
    }
    .tab-default {
      .el-tabs__content {
        // height: calc(100% - 92px);
        height: calc(100% - 70px);
        //多层tabs
        .el-tabs__content {
          height: calc(100% - 51px);
          .el-main {
            padding: 0px 0px 0px 20px;
            .ht-table-panel {
              margin: 0;
            }
          }
          .ht-table-panel {
            padding: 0;
          }
        }
        .not-aside-tree {
          .el-tabs__content {
            .ht-table-panel {
              margin: 0;
            }
          }
        }
      }
    }
  }
}
.ht-menu_title {
  display: flex;
  ::v-deep.collect-icon {
    padding-left: 6px;
  }
}
::v-deep {
  //列表上下24px全局设置
  .el-tabs__content {
    .el-container {
      .el-main {
        padding: 9px 20px 24px 20px;
      }
      .ht-table-panel {
        padding: 0;
      }
    }
    .not-aside-tree {
      height: calc(100% - 24px);
    }
  }

  .el-tabs--card > .el-tabs__header .el-tabs__nav {
    background-color: rgba(248, 248, 248, 0.39);
  }
  // .el-tabs--card > .el-tabs__header .el-tabs__item {
  //   // border: 0;
  // }
  .el-tabs--card > .el-tabs__header .el-tabs__item.is-active {
    background-color: #fff;
    span {
      .ht-menu_title {
        // border-bottom: 3px solid #409eff;
        height: 37px;
      }
    }
  }
  .el-tabs__nav-wrap::after {
    height: 1px;
  }
  .tab-default {
    .el-tabs__item.is-top:nth-child(2) {
      padding-left: 30px;
    }
    .el-tabs__item {
      height: 56px;
      line-height: 56px;
    }
    .el-tabs--card {
      .el-tabs__item {
        height: 40px;
        line-height: 40px;
      }
    }
  }
}
</style>
